package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.CustomAudioData
import com.canbot.u05.activity.isNotEmpty2
import kotlinx.android.synthetic.main.custom_audio_item.view.*

/**
 * Created by liu.heng on 2018/5/25.
 */

class CustomAudioAdapter(context: Context, private val mList: List<CustomAudioData>?, private val mOnItemClickListener: OnItemClickListener) : RecyclerView.Adapter<CustomAudioAdapter.ViewHolder>() {

    private val mLayoutInflater: LayoutInflater = LayoutInflater.from(context)

    /**设定一个选中的标志位，在activity中传入值*/
    var checked = -1

    interface OnItemClickListener {

        fun onItemClick(view: View)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflate = mLayoutInflater.inflate(R.layout.custom_audio_item, parent, false)
        return ViewHolder(inflate)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        if (checked == position) {
            holder.checkImage.visibility = View.VISIBLE
        } else {
            holder.checkImage.visibility = View.GONE
        }
        if (mList.isNotEmpty2()) holder.musicText.text = mList[position].name
        holder.mRelativeLayout.tag = position
        holder.mRelativeLayout.setOnClickListener { view -> mOnItemClickListener.onItemClick(view) }
    }

    override fun getItemCount(): Int {
        return mList?.size ?: 0
    }


    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        val mRelativeLayout: RelativeLayout = view.my_relativeLayout

        val checkImage: ImageView = view.check_image

        val musicText: TextView = view.music_text

    }
}
